<template>
  <transition name="showHeader">
    <div v-if="visible" class="header-animat">
      <a-layout-header
        v-if="visible"
        :class="[
          fixedHeader && 'ant-header-fixedHeader',
          sidebarOpened ? 'ant-header-side-opened' : 'ant-header-side-closed'
        ]"
        :style="{ padding: '0' }"
      >
        <div
          style="
            position: relative;
            display: flex;
            justify-content: space-between;
            padding-left: 20px;
          "
          v-if="mode === 'sidemenu'"
          class="header"
        >
          <!-- <div class="platform-list">
            <div
              class="platform-item"
              :class="{ active: index == platformIndex }"
              v-for="(item, index) in platformList"
              :key="index"
              @click="togglePlatform(index)"
            >
              {{ item }}
            </div>
          </div> -->
          <!-- <a-icon
            v-if="device === 'mobile'"
            class="trigger"
            :type="collapsed ? 'menu-fold' : 'menu-unfold'"
            @click="toggle"
          />
          <a-icon
            v-else
            class="trigger"
            :type="collapsed ? 'menu-unfold' : 'menu-fold'"
            @click="toggle"
          /> -->
          <!-- <div @click="()=>isSelect=!isSelect" class="global_header_select">{{ use_name }}</div>
          <img @click="()=>isSelect=!isSelect" style="width:10px;height:6px;margin-left:10px;" src="https://zlj-xft-test.oss-cn-shenzhen.aliyuncs.com/091b1401-c6a3-43ea-b640-d581c5c123be.png" alt=""> -->
          <logo
            :style="`width:458px`"
            :close="!collapsed"
          />
          <user-menu></user-menu>
        </div>
        <div v-else :class="['top-nav-header-index', theme]">
          <div class="header-index-wide">
            <div class="header-index-left">
              <logo class="top-nav-header" :show-title="device !== 'mobile'" />
              <s-menu
                v-if="device !== 'mobile'"
                mode="horizontal"
                :menu="menus"
                :theme="theme"
              />
              <a-icon
                v-else
                class="trigger"
                :type="collapsed ? 'menu-fold' : 'menu-unfold'"
                @click="toggle"
              />
            </div>
            <user-menu class="header-index-right"></user-menu>
          </div>
        </div>
      </a-layout-header>
    </div>
  </transition>
</template>

<script>
import UserMenu from '../tools/UserMenu'
import SMenu from '../Menu/'
import Logo from '../tools/Logo'
import { mixin } from '@/utils/mixin'
import storage from 'store'

export default {
  name: 'GlobalHeader',
  components: {
    UserMenu,
    SMenu,
    Logo
  },
  mixins: [mixin],
  props: {
    mode: {
      type: String,
      // sidemenu, topmenu
      default: 'sidemenu'
    },
    menus: {
      type: Array,
      required: true
    },
    theme: {
      type: String,
      required: false,
      default: ''
    },
    collapsed: {
      type: Boolean,
      required: false,
      default: false
    },
    device: {
      type: String,
      required: false,
      default: 'desktop'
    },
    isSelect: {
      type: Boolean,
      required: false,
      default: false
    }
  },
  data () {
    return {
      use_code: '',
      use_name: '',
      use_level: '1',
      // isSelect: false,
      visible: true,
      oldScrollTop: 0,
      storeList: [],
      permission_level: 1,
      platformList: ['运营平台', '账户平台', '营销平台'],
      platformIndex: 0
    }
  },
  mounted () {
    document.addEventListener('scroll', this.handleScroll, { passive: true })
  },
  methods: {
    checkPlatform (e) {
      this.use_level = 1
      this.use_code = e.code
      this.use_name = e.name
      storage.set('use_level', 1)
      storage.remove('global_store_code')
      storage.remove('global_store_name')
      storage.remove('global_merchant_code')
      storage.remove('global_merchant_name')
      storage.set('industry_code', '')
      // this.isSelect = false
      this.toggleSelect()
      this.$router.push({
        path: '/'
      })
      this.$emit('contents')
    },
    checkStore (e, merchantInfo) {
      console.log(e)
      this.use_level = 3
      this.use_code = e.code
      this.use_name = e.name
      storage.set('global_store_code', e.code)
      storage.set('global_store_name', e.name)
      if (e.industry_code) {
        storage.set('industry_code', e.industry_code.join(','))
      } else {
        storage.set('industry_code', '')
      }

      if (merchantInfo) {
        storage.set('global_merchant_code', merchantInfo.code)
        storage.set('global_merchant_name', merchantInfo.name)
      } else {
        storage.set('global_merchant_code', e.merchantCode)
        storage.set('global_merchant_name', e.merchantName)
      }

      // storage.remove('global_merchant_code')
      // storage.remove('global_merchant_name')
      storage.set('use_level', 3)

      // this.isSelect = false
      this.toggleSelect()
      this.$router.push({
        path: '/'
      })
      this.$emit('contents')
    },
    checkMerchant (e) {
      this.use_level = 2
      this.use_code = e.code
      this.use_name = e.name
      storage.set('global_merchant_code', e.code)
      storage.set('global_merchant_name', e.name)
      storage.remove('global_store_code')
      storage.remove('global_store_name')
      storage.set('use_level', 2)
      storage.set('industry_code', '')

      // this.isSelect = false
      this.toggleSelect()
      this.$router.push({
        path: '/'
      })
      this.$emit('contents')
    },
    handleScroll () {
      if (!this.autoHideHeader) {
        return
      }

      const scrollTop =
        document.body.scrollTop + document.documentElement.scrollTop
      if (!this.ticking) {
        this.ticking = true
        requestAnimationFrame(() => {
          if (this.oldScrollTop > scrollTop) {
            this.visible = true
          } else if (scrollTop > 300 && this.visible) {
            this.visible = false
          } else if (scrollTop < 300 && !this.visible) {
            this.visible = true
          }
          this.oldScrollTop = scrollTop
          this.ticking = false
        })
      }
    },
    toggle () {
      this.$emit('toggle')
    },
    toggleSelect () {
      this.$emit('toggleSelect')
    },
    togglePlatform (index) {
      this.platformIndex = index
    }
  },
  beforeDestroy () {
    document.body.removeEventListener('scroll', this.handleScroll, true)
  }
}
</script>

<style lang="less">
@import '../index.less';
.global_header_select_box_item_3_content {
  height: 40px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  cursor: pointer;
  color: #666666;
  width: 210px;
}
.global_header_select_box_item_3_content.height0 {
  height: 0 !important;
}
.global_header_select_box_item_3_content.active {
  background: #f0f2ff;
  border-radius: 4px;
  color: #4393F2;
}

.global_header_select_box_item_3 {
  padding-left: 20px;
}
.global_header_select_box_contro {
  height: 360px;
  overflow-y: scroll;
  padding-right: 20px;
}
.global_header_select_box_item_3_name {
  height: 24px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  line-height: 24px;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.global_header_select_box_item_2_content {
  display: flex;
  align-items: center;
  height: 40px;
  padding-left: 20px;
}
.global_header_select_box_item_2 {
  padding-left: 30px;
}
.global_header_select_box_item_2.icon {
  padding: 0;
  .global_header_select_box_item_2_item.icon {
    background: url('https://91xft-static.oss-cn-hangzhou.aliyuncs.com/xft-cli/tg/fuwuquyu-0510.png')
      no-repeat 0 10px;
    background-size: 18px 22px;
    padding-left: 20px;
    .global_header_select_box_item_2_content {
      height: 40px;
      width: 230px;
      color: #333333;
      padding-left: 10px !important;
      margin-left: 10px;
    }
    .global_header_select_box_item_2_content.active {
      background: #f0f2ff;
      border-radius: 4px;
      color: #4393F2;
    }
  }
}

.global_header_select_box_item_2_name {
  height: 24px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  line-height: 24px;
}
.global_header_select_box_item_1_la {
  margin-left: auto;
  width: 20px;
  height: 100%;
  display: flex;
  align-items: center;
  cursor: pointer;
  background: #fff;
}
.global_header_select_box_item_1_name {
  flex: 1;
  height: 24px;
  font-size: 14px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #333333;
  line-height: 24px;
  cursor: pointer;
}
.global_header_select_box_item_1 {
  display: flex;
  align-items: center;
  height: 40px;
  width: 100% !important;
}
.global_header_select_box_ser {
  display: flex;
  align-items: center;
  height: 47px;
  margin-right: 20px;
  border-bottom: 1px solid #eeeeee;
}
.global_header_select_box {
  position: absolute;
  left: 100px;
  top: 80px;
  padding: 0 0 0 20px;
  width: 320px;
  height: 410px;
  background: #ffffff;
  border-radius: 5px;
  box-shadow: 0px 9px 28px 8px rgba(0, 0, 0, 0.05),
    0px 6px 16px 0px rgba(0, 0, 0, 0.08), 0px 3px 6px -4px rgba(0, 0, 0, 0.12);
}
.global_header_select {
  display: inline-block;
  margin-left: 40px;
  height: 22px;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #333333;
  line-height: 22px;
  cursor: pointer;
}
.header-animat {
  position: relative;
  z-index: @ant-global-header-zindex;
      box-shadow: 0px 2px 6px 0px rgba(0,5,47,0.12);
}
.showHeader-enter-active {
  transition: all 0.25s ease;
}
.showHeader-leave-active {
  transition: all 0.5s ease;
}
.showHeader-enter,
.showHeader-leave-to {
  opacity: 0;
}
.platform-list {
  display: flex;
  margin-left: 50px;
  .platform-item {
    margin: 0 20px;
    font-size: 14px;
    font-weight: 400;
    cursor: pointer;
  }
  .platform-item:hover {
    color: #4393F2;
  }
}
</style>
